package com.ruoyi.dm.component;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.utils.biz.PathUtil;
import com.ruoyi.dm.component.manager.ShapeDataManager;
import com.ruoyi.dm.domain.BO.ShapeDataBO;
import com.ruoyi.dm.domain.po.SpaceTrackCollectRecordPO;
import common.SensorPar;
import lombok.extern.slf4j.Slf4j;
import org.orekit.data.DataContext;
import org.orekit.data.DataProvidersManager;
import org.orekit.data.DirectoryCrawler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import plugins.ImportFile;

import java.io.File;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 空间目标数据 姿态数据
 */
@Component
@Slf4j
public class ShapeGracefoStrategy implements DataStrategy<SpaceTrackCollectRecordPO> {

    @Autowired
    private ShapeDataManager dataManager;

    @Override
    public SpaceTrackCollectRecordPO makeupPo(SpaceTrackCollectRecordPO po) {
        //SCA1B_2022-01-02_D_04.txt
        String fileName = po.getFileName();
        String[] split = fileName.split("_");
        //po.setDataType(split[1]);
        DateTime date = DateUtil.parse(split[1], "yyyy-MM-dd");
        po.setFileDate(date);
        return po;
    }

    @Override
    public String pythonCmd(String satellite, String dataType, String filePath, String method, Integer year) {

        return null;
    }


    @Override
    public void save(String satellite, String outFile) throws Exception {

        //需先加载星历数据
        String dataPath = PathUtil.configDir() + "/collect/orekit";
        File orekitData = new File(dataPath);
        DataProvidersManager manager = DataContext.getDefault().getDataProvidersManager();
        manager.addProvider(new DirectoryCrawler(orekitData));


        //输入数据,卫星轨道数据
        List<SensorPar.StQuaternion> satOrbitDataLst= new ArrayList<>();
        ImportFile.readGracefoAttData(outFile,satOrbitDataLst);
        log.info("{} 读取 size {}",outFile,satOrbitDataLst.size());
        List<ShapeDataBO> resList = new ArrayList<>(satOrbitDataLst.size());
        for (SensorPar.StQuaternion point : satOrbitDataLst) {
            ShapeDataBO build = ShapeDataBO.builder().qx(point._qx).qy(point._qy).qz(point._qz).qs(point._qs).epoch_time(DateUtil.parse(point.strEpoch,"yyyy-MM-dd HH:mm:ss")).build();
            resList.add(build);
        }
        satOrbitDataLst = null;
        //数据入库
        dataManager.saveData(satellite,resList);
    }

}
